package com.cy.module;

import com.cy.ext.plugin.route.ControllerBind;
import com.cy.model.LicenseLog;
import com.jfinal.plugin.activerecord.Page;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;

@ControllerBind(controllerKey = "/clmp/licenseLog")
public class LicenseLogController extends WebApiController {

    @Override
    public void index() {
        LicenseLog licenseLog = getModel(LicenseLog.class);
        int page = getParaToInt("pageIndex");
        int pageSize = getParaToInt("limit");
        Map<String, String[]> parasMap = getParaMap();
        if (null != parasMap){
            String start = parasMap.get("start_time")[0];
            String end = parasMap.get("end_time")[0];
            if (StringUtils.isNotBlank(start)){
                licenseLog.put("start",start);
            }
            if (StringUtils.isNotBlank(end)){
                licenseLog.put("end",end);
            }
        }

        Page<LicenseLog> licenseLogPage = LicenseLog.dao.paginate(page, pageSize, LicenseLog.SELECT_ALL, licenseLog);
        renderBuiPage(licenseLogPage, 200);
    }

    @Override
    public void save() {
        LicenseLog licenseLog = getModel(LicenseLog.class);
        if (licenseLog.save()) {
            renderSuccess();
        } else {
            renderFail();
        }
    }
}
